package logmatcher;


import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;

import java.lang.reflect.Field;

import java.util.Iterator;


/**
 * Various test helpers.
 *
 * @version $Revision: 548 $
*/
public final class TstUtils {
    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
     * Creates a new TestUtils object.
     */
    private TstUtils() {
        super();
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     * Get value of private field in an object.
     *
     * @param target object to be introspected.
     * @param fieldName name of the (private) field.
     *
     * @return field value.
     *
     * @throws SecurityException if
     * @throws NoSuchFieldException if object do not contain such field.
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Object getPrivateField(Object target, String fieldName)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Class c = target.getClass();
        Field f = c.getDeclaredField(fieldName);
        f.setAccessible(true);

        return f.get(target);
    }


    /**
     * Print configuration to specified logger.
     *
     * @param logger target Log instance.
     * @param config Configuration to be printed.
     */
    public static void printConfiguration(Log logger, Configuration config) {
        for (Iterator it = config.getKeys(); it.hasNext();) {
            String key = it.next().toString();
            logger.info(key + "=" + config.getProperty(key));
        }
    }

    //~ Inner Classes --------------------------------------------------------------------------------------------------

    /**
     * DOCUMENT ME!
     *
     * @version $Revision: 548 $
    */
    public static class DummyFilter implements InputFilter, Configurable {
        //~ Instance fields --------------------------------------------------------------------------------------------

        /** DOCUMENT ME! */
        public int testValue = 0;

        //~ Methods ----------------------------------------------------------------------------------------------------

        /**
         * DOCUMENT ME!
         *
         * @param message DOCUMENT ME!
         *
         * @return DOCUMENT ME!
         */
        public boolean accept(Object message) {
            return false;
        }


        /**
         * DOCUMENT ME!
         *
         * @param config DOCUMENT ME!
         * @param id DOCUMENT ME!
         *
         * @throws InitializationException DOCUMENT ME!
         */
        public void configure(Configuration config, String id)
            throws InitializationException {
            testValue = 101;
        }
    }
}
